﻿using System;
using System.Web;
using System.Linq;
using System.Net;
using System.Text;
using System.Collections;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using System.Runtime.InteropServices;
using StripeOne.Core.SoCache;
using StripeOne.Core.Tools;

namespace StripeOne.Core.XmlConfig
{
	public static class GeneralTools
	{

		#region Get's Context Aplicattion Propeties

		public static string GetConfigFolderPath()
		{
			return AppDomain.CurrentDomain.BaseDirectory + @"Config\";
		}

		#endregion

		#region Add's Remove's Slash

		public static string AddBeginSlash(string value)
		{
			if(String.IsNullOrEmpty(value) || value.Equals("/")) return "/";
			value.Replace("//", "/").Replace("//", "/");
			return value[0].Equals('/') ? value : ("/" + value);
		}
		public static string AddEndSlash(string value)
		{
			if(String.IsNullOrEmpty(value)) return "/";
			return value[value.Length - 1].Equals('/') ? value : value + '/';
		}

		public static string RemoveBeginSlash(string value)
		{
			if(String.IsNullOrEmpty(value)) return String.Empty;

			value.Replace("//", "/").Replace("//", "/");
			value = value.Equals("/") ? String.Empty : value;
			if(value != String.Empty)
			{
				value = value[0].Equals('/') ? (value.Length > 1 ? value.Substring(1) : String.Empty) : value;
			}
			return value;
		}
		public static string RemoveEndSlash(string value)
		{
			if(String.IsNullOrEmpty(value)) return String.Empty;

			value.Replace("//", "/").Replace("//", "/");
			value = value.Equals("/") ? String.Empty : value;
			if(value != String.Empty)
			{
				value = value[value.Length - 1].Equals('/') ? value.Substring(0, value.Length - 1) : value;
			}
			return value;
		}

		#endregion

		#region ArrayContains

		public static bool ArrayContains(int val, int[] array)
		{
			foreach(int arrayValue in array)
			{
				if(val == arrayValue)
					return (true);
			}
			return (false);
		}

		public static bool ArrayContains(string val, string[] array)
		{
			return ArrayContains(val, array, true);
		}

		public static bool ArrayContains(string val, string[] array, bool caseSensitive)
		{
			foreach(string arrayValue in array)
			{
				if(caseSensitive)
				{
					if(val == arrayValue)
						return (true);
				}
				else
				{
					if(val.ToLower() == arrayValue.ToLower())
						return (true);
				}
			}
			return (false);
		}



		#region Summary
		/// <summary>
		/// Looks for a value in an array
		/// </summary>
		/// <param name="val">Value to be searched</param>
		/// <param name="array">Array</param>
		/// <returns>
		/// True - if the array contains the value. 
		/// False - if the array doesn't contain the value
		/// </returns>
		#endregion
		public static bool ArrayContains(char val, char[] array)
		{
			foreach(char arrayValue in array)
			{
				if(val == arrayValue)
					return (true);
			}
			return (false);
		}

		#endregion

		#region ValidateEmail
		public static bool ValidateEmail(string emailAddress)
		{
			return Regex.IsMatch(emailAddress, @"^((([a-z]|[0-9]|!|#|$|%|&|'|\*|\+|\-|/|=|\?|\^|_|`|\{|\||\}|~)+(\.([a-z]|[0-9]|!|#|$|%|&|'|\*|\+|\-|/|=|\?|\^|_|`|\{|\||\}|~)+)*)@((((([a-z]|[0-9])([a-z]|[0-9]|\-){0,61}([a-z]|[0-9])\.))*([a-z]|[0-9])([a-z]|[0-9]|\-){0,61}([a-z]|[0-9])\.(af|ax|al|dz|as|ad|ao|ai|aq|ag|ar|am|aw|au|at|az|bs|bh|bd|bb|by|be|bz|bj|bm|bt|bo|ba|bw|bv|br|io|bn|bg|bf|bi|kh|cm|ca|cv|ky|cf|td|cl|cn|cx|cc|co|km|cg|cd|ck|cr|ci|hr|cu|cy|cz|dk|dj|dm|do|ec|eg|sv|gq|er|ee|et|fk|fo|fj|fi|fr|gf|pf|tf|ga|gm|ge|de|gh|gi|gr|gl|gd|gp|gu|gt| gg|gn|gw|gy|ht|hm|va|hn|hk|hu|is|in|id|ir|iq|ie|im|il|it|jm|jp|je|jo|kz|ke|ki|kp|kr|kw|kg|la|lv|lb|ls|lr|ly|li|lt|lu|mo|mk|mg|mw|my|mv|ml|mt|mh|mq|mr|mu|yt|mx|fm|md|mc|mn|ms|ma|mz|mm|na|nr|np|nl|an|nc|nz|ni|ne|ng|nu|nf|mp|no|om|pk|pw|ps|pa|pg|py|pe|ph|pn|pl|pt|pr|qa|re|ro|ru|rw|sh|kn|lc|pm|vc|ws|sm|st|sa|sn|cs|sc|sl|sg|sk|si|sb|so|za|gs|es|lk|sd|sr|sj|sz|se|ch|sy|tw|tj|tz|th|tl|tg|tk|to|tt|tn|tr|tm|tc|tv|ug|ua|ae|gb|us|um|uy|uz|vu|ve|vn|vg|vi|wf|eh|ye|zm|zw|com|edu|gov|int|mil|net|org|biz|info|name|pro|aero|coop|museum|arpa))|(((([0-9]){1,3}\.){3}([0-9]){1,3}))|(\[((([0-9]){1,3}\.){3}([0-9]){1,3})\])))$");
		}
		#endregion

		public static string ToCacheKeyFormat(string key)
		{
			key = key.Replace("http://", "http").Replace("https://", "https").Replace("ftp://", "ftp")
					 .Replace("&", "").Replace("#", "").Replace("@", "").Replace("?", "")
					 .Replace("//", "/").Replace("//", "/")
					 .Replace("\\", "/").Replace("\\", "/")
					 .Replace("/", "_").Replace("\n", "");
			return key;
		}
	}
}
